package com.byt.common.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.byt.base.BaseApplication;
import com.byt.base.utils.DisplayUtil;
import com.byt.common.R;

import java.io.File;
import java.util.concurrent.ExecutionException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Description : 图片加载工具类 使用glide框架封装
 * Glide加载调用
 */
public class ImageLoaderUtils {

    /**
     * 正规正方形的加载
     * 加载默认的图片个错误图片默认
     *
     * @param imageView
     * @param url
     */
    public static void display(ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(R.drawable.ic_image_loading)
                .error(R.drawable.ic_image_loading)
                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void display(ImageView imageView, int resid) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(resid)
                .placeholder(resid)
                .error(resid)
                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    /**
     * 正规正方形的加载
     * 需要传入默认的错误图片
     *
     * @param imageView
     * @param url
     * @param error
     */
    public static void displayError(ImageView imageView, String url, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(R.drawable.ic_image_loading)
                .error(error)
                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    /**
     * 正规正方形的加载
     * 需要传入默认的错误图片
     *
     * @param imageView
     * @param url
     * @param error
     */
    public static void displayFitError(ImageView imageView, String url, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(R.drawable.ic_image_loading)
                .error(error)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    /**
     * 正规正方形的加载
     * 需要传入默认的加载图片
     *
     * @param imageView
     * @param url
     * @param placeholder
     */
    public static void displayPlace(ImageView imageView, String url, int placeholder) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(placeholder)
                .error(R.drawable.ic_image_loading)
                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    /**
     * 两种都是需要传入的默认加载和错误加载
     *
     * @param imageView
     * @param url
     * @param placeholder
     * @param error
     */
    public static void displayPE(ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(placeholder)
                .error(error)
                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void displayFitPE(ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(placeholder)
                .error(error)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void displayFitPE(ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void displayCircle(ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }

        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(R.drawable.ic_circle_loading)
                .error(R.drawable.ic_circle_loading)
                .dontAnimate()
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransformation(BaseApplication.mApplication))
                .into(imageView);
    }

    public static void displayCircle(ImageView imageView, int res) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(res)
                .placeholder(res)
                .error(res)
                .dontAnimate()
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransformation(BaseApplication.mApplication))
                .into(imageView);
    }

    public static void displayCircle(ImageView imageView, String url, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(R.drawable.ic_circle_loading)
                .error(error)
                .dontAnimate()
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransformation(BaseApplication.mApplication))
                .into(imageView);
    }

    public static void displayCircle(ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(placeholder)
                .error(error)
                .dontAnimate()
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransformation(BaseApplication.mApplication))
                .into(imageView);
    }

    public static void displayCircles(ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(placeholder)
                .error(error)
                .dontAnimate()
                .transform(new GlideCircleTransformation(BaseApplication.mApplication))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void displayBitmap(ImageView imageView, String url, int resId) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .asBitmap()
                .load(url)
                .placeholder(resId)
                .error(resId)
                .dontAnimate()
                .format(DecodeFormat.PREFER_ARGB_8888)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransformation(BaseApplication.mApplication))
                .into(imageView);
    }

    public static void displayBlur(Context context, ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(BaseApplication.mApplication)
                .load(url)
                .placeholder(placeholder)
                .error(error)
                .dontAnimate()
                .transform(new BlurTransformation(context, 10, 10))
                .into(imageView);
    }

    /**
     * 自适应宽度加载图片。保持图片的长宽比例不变，通过修改imageView的高度来完全显示图片。
     */
    public static void disPlayMetris(Context context, ImageView imageView, String url) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new ViewTarget<ImageView, Bitmap>(imageView) {
                    @Override
                    public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
//                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        int vw = DisplayUtil.getScreenWidth(context) - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) bitmap.getWidth();
                        int vh = Math.round(bitmap.getHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        imageView.setImageBitmap(bitmap);
                    }

                });
    }

    /**
     * 自适应宽度加载图片。保持图片的长宽比例不变，通过修改imageView的高度来完全显示图片。
     */
    public static void disPlayMetrisChat(Context context, ImageView imageView, String url) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new ViewTarget<ImageView, Bitmap>(imageView) {
                    @Override
                    public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                        int imageWidth = bitmap.getWidth();
                        int imageHeight = bitmap.getHeight();
                        int height = imageHeight;
                        int width = imageWidth;
                        if (imageWidth >= ((int) context.getResources().getDimension(R.dimen.x400))) {
                            height = ((int) context.getResources().getDimension(R.dimen.x400)) * imageHeight / imageWidth;
                            width = (int) context.getResources().getDimension(R.dimen.x400);
                        }
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(width, height));
                        imageView.setLayoutParams(lp);
                        imageView.setImageBitmap(bitmap);
                    }
                });
    }


    public static Bitmap loadImageSimpleBitmap(String url, int dimenId) {
        Bitmap myBitmap = null;
        try {
            myBitmap = Glide.with(BaseApplication.mApplication)
                    .asBitmap() //必须
                    .load(url)
                    .centerCrop()
                    .submit((int) BaseApplication.mApplication.getResources().getDimension(dimenId),
                            (int) BaseApplication.mApplication.getResources().getDimension(dimenId))
                    .get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return myBitmap;
    }

    public static File loadImageSizeBitmap(String url) {
        File file = null;
        FutureTarget<File> target =  Glide.with(BaseApplication.mApplication)
                .asFile()
                .load(url)
                .submit();
        try {
            file = target.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 加载网络图片(带圆角)
     *
     * @param context
     * @param iv
     * @param url
     * @param radius
     */
    public static void loadImageRadius(Context context, ImageView iv, String url, int radius, int resId) {
        if (null == context || null == iv) {
            return;
        }
        if (TextUtils.isEmpty(url)) {
            try {
                Glide.with(context).load(resId).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
                        transform(new GlideRoundTransformation(context, radius))
                        .into(iv);
            } catch (Exception e) {
            }
        } else {
            try {
                Glide.with(context).load(url).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
                        transform(new GlideRoundTransformation(context, radius))
                        .placeholder(resId).into(iv);
            } catch (Exception e) {

            }
        }
    }
}